using UnityEngine;
using System.Diagnostics;
using System.Collections.Generic;

public class ProfilerLog : System.IDisposable
{
    private Stopwatch m_stopwatch;
    private string m_tag;
    public ProfilerLog(string tag)
    {
        m_tag = tag;
        m_stopwatch = new Stopwatch();
        m_stopwatch.Start();
    }

    public void Dispose()
    {
        m_stopwatch.Stop();
        long elapsedms = m_stopwatch.ElapsedMilliseconds;
        UnityEngine.Debug.Log(string.Format("tag:{0} use time:{1}", m_tag, elapsedms));
    }
}

public class BattleLog
{
    public static bool sOpenLog = false;
    private static int id = 0;
    private static List<string> m_logStrList = new List<string>();
    private static int s_index = 0;

    public static void Print(string str)
    {
        if (sOpenLog)
            m_logStrList.Add(str);
    }

    public static void CleanLogs()
    {
        m_logStrList.Clear();
    }

    public static void SaveLogToFile()
    {
#if UNITY_EDITOR
        if (m_logStrList.Count > 0)
        {
            string path = Application.dataPath + "/Resources/" + "debug" + s_index + ".txt";
            System.IO.StreamWriter writer = null;
            if (!System.IO.File.Exists(path))
                writer = System.IO.File.CreateText(path);
            else
                writer = new System.IO.StreamWriter(path, false, System.Text.Encoding.UTF8);

            string fileString = string.Empty;
            for (int i = 0; i < m_logStrList.Count; i++)
            {
                fileString += (m_logStrList[i] + "\n");
            }
            writer.Write(fileString);
            writer.Close();

            UnityEditor.AssetDatabase.SaveAssets();
            UnityEditor.AssetDatabase.Refresh();
            s_index++;
        }
#endif
        m_logStrList.Clear();
    }
}

